package ${packageName};

import com.wuyan.web.base.helper.PageHelper;
import com.wuyan.web.base.helper.rep.RepBody;
import com.wuyan.web.base.helper.rep.RepHelper;
import com.wuyan.web.base.helper.rep.RepPageData;
import com.wuyan.web.base.helper.rep.RepCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.querydsl.core.BooleanBuilder;
import org.springframework.data.domain.Pageable;
import ${entity.packageName}.${entity.className};
import ${lastRenderResponse.form.packageName}.${lastRenderResponse.form.className};
import ${lastRenderResponse.service.packageName}.${lastRenderResponse.service.className};

<#list imports as import>
<#if import!="java.lang.Integer">
import ${import};
</#if>
</#list>

import java.security.Principal;
import java.util.Optional;

/**
 * controller for ${entity.className} generated by jpa-codegen
 * ${comments}
 * 接口遵循restful规范，其中post用于新增，get用于查询数据，put用于修改，delete用于删除，默认增加分页查询接口
 *
 * @author ${author}
 * @date ${date}
 */

@RestController
@RequestMapping("${otherParams.url_prefix}/${entity.className?replace("([a-z])([A-Z]+)", "$1-$2", "r")?lower_case}")
public class ${className} implements RepHelper {

    @Autowired
    private ${lastRenderResponse.service.className} ${lastRenderResponse.service.className?uncap_first};

    @PostMapping
    public RepBody<${entity.className}> create(@RequestBody @Validated ${lastRenderResponse.form.className} form) {
        ${entity.className} ${entity.className?uncap_first} = ${lastRenderResponse.service.className?uncap_first}.create(form);
        return ok(${entity.className?uncap_first});
    }

    @DeleteMapping("/{id}")
    public RepBody<${entity.className}> delete(@PathVariable("id") ${entity.id.className} id) {
        ${lastRenderResponse.service.className?uncap_first}.delete(id);
        return ok();
    }

    @PutMapping("/{id}")
    public RepBody<${entity.className}> update(@RequestBody @Validated ${lastRenderResponse.form.className} form,
                                    @PathVariable("id") ${entity.id.className} id) {
        Optional<${entity.className}> ${entity.className?uncap_first} = ${lastRenderResponse.service.className?uncap_first}.update(form, id);
        return ${entity.className?uncap_first}.isPresent() ? ok(${entity.className?uncap_first}.get()) : error(RepCodeEnum.ERR_NO_ID);
    }

    @GetMapping("/{id}")
    public RepBody<${entity.className}> get(@PathVariable("id") ${entity.id.className} id) {
        Optional<${entity.className}> ${entity.className?uncap_first} = ${lastRenderResponse.service.className?uncap_first}.get(id);
        return ${entity.className?uncap_first}.isPresent() ? ok(${entity.className?uncap_first}.get()) : error(RepCodeEnum.ERR_NO_ID);
    }

    @GetMapping
    public RepBody<RepPageData<${entity.className}>> page(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                          @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
                                                          Principal principal) {
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        Pageable pageable = PageHelper.page(page, limit, false);

        // TODO 业务逻辑
        // TODO 业务逻辑

        return ok(${lastRenderResponse.service.className?uncap_first}.page(booleanBuilder, pageable));
    }

}